package com.voice.call.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.entity.Region;
import com.voice.call.mapper.RegionMapper;
import com.voice.call.service.VoiceRateService;
import com.voice.call.utils.JSONUtil;
import com.voice.call.utils.StringUtil;
import com.voice.call.version.ApiVersion;


//资费 
@RestController
@RequestMapping("/api/{version}/voiceRate/")
public class VoiceRateController {

	private static final Logger log = LoggerFactory.getLogger(VoiceRateController.class);
	
	@Resource
	private VoiceRateService voiceRateService;
	
	@Resource
	private RegionMapper regionMapper;
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result list(HttpServletRequest request,@RequestBody Map<String, Object> params){
		
		//log.debug("参数:{}",JSONUtil.objectToJson(params));
		String lang = ConstLang.getLang(params);
		//return voiceRateService.getVoiceRateList();
		
		List<Map<String, Object>> rsList = new ArrayList<Map<String,Object>>();
		List<Region> list = regionMapper.getVoiceRateList(lang);
		if (list!=null && !list.isEmpty()) {
			
			for (Region region : list) {
				Map<String, Object> itemMap = new HashMap<String, Object>();
				itemMap.put("id", region.getId());
				itemMap.put("name", region.getName());
				itemMap.put("prefix", region.getAreaCode());
				itemMap.put("country_pic", region.getCountryPic());
				rsList.add(itemMap);
			}
			
		}
		return new Result(rsList,lang);
		
		
	}
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "search", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result searchVoiceRate(HttpServletRequest request,@RequestBody Map<String, Object> params){
		
		//log.debug("参数:{}",JSONUtil.objectToJson(params));
		String lang = ConstLang.getLang(params);
		// 判断入参
		if (null == params || params.get("keyword") == null || StringUtil.isEmpty(params.get("keyword").toString())) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		
		Result rs = voiceRateService.getSearchVoiceRateList(params.get("keyword").toString(),lang);
		rs.changeLange(lang);
		return rs;
	}
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "hot", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result topVoiceRateList(HttpServletRequest request,@RequestBody Map<String, Object> params){
		
		//log.debug("参数:{}",JSONUtil.objectToJson(params));
		String lang = ConstLang.getLang(params);
		// 判断入参
		if (null == params || params.get("uid") == null ) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		Result rs = voiceRateService.getHotVoiceRateList(lang);
		rs.changeLange(lang);
		return rs;
	}
}
